import 'package:flutter/material.dart';
import 'package:flutter_eve_plugin/components/health/sys_child_page.dart';
import 'package:flutter_eve_plugin/components/main/app_bar.dart';
import 'package:flutter_eve_plugin/components/main/tab_bar.dart';
import 'package:flutter_eve_plugin/model/health/sys_health_property.dart';
import 'package:flutter_eve_plugin/store/main.dart';
import 'package:flutter_eve_plugin/store_vm/app_vm.dart';
import 'package:flutter_redux/flutter_redux.dart';

class SysStateMainPage extends StatefulWidget {
  final String title;

  SysStateMainPage({Key key, this.title}) : super(key: key);

  @override
  _SysStateMainPageState createState() => _SysStateMainPageState();
}

class _SysStateMainPageState extends State<SysStateMainPage>
    with SingleTickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, AppStateViewModel>(
        converter: (store) => AppStateViewModel(
              state: store.state,
            ),
        builder: (context, vm) {
          return DefaultTabController(
              length: tabList().length,
              initialIndex: vm.state.headlthState.sysCurrPageIndex,
              child: Scaffold(
                appBar: buildAppBar(context,
                    bottom: buildTabBar(tabList()), title: widget.title),
                body: TabBarView(
                  physics: BouncingScrollPhysics(),
                  children: childrenTab(vm),
                ),
              ));
        });
  }

  List<Widget> childrenTab(AppStateViewModel vm) {
    return <Widget>[
      SysChildPageWidget(
        type: 0,
        info: tabItemData(vm, '呼吸系统'),
      ),
      SysChildPageWidget(
        type: 1,
        info: tabItemData(vm, '消化系统'),
      ),
      SysChildPageWidget(
        type: 2,
        info: tabItemData(vm, '运动系统'),
      ),
      SysChildPageWidget(
        type: 3,
        info: tabItemData(vm, '神经系统'),
      ),
      SysChildPageWidget(
        type: 4,
        info: tabItemData(vm, '内分泌系统'),
      ),
    ];
  }

  List<Widget> tabList() {
    return [
      Tab(
        text: '呼吸',
      ),
      Tab(
        text: '消化',
      ),
      Tab(
        text: '运动',
      ),
      Tab(
        text: '神经',
      ),
      Tab(
        text: '内分泌',
      ),
    ];
  }

  PropertyHealthDataInfo tabItemData(AppStateViewModel vm, String key) {
    var list = vm.state.headlthState.propertyHealthModel.propertyHealthData
        ?.where((item) {
      return item.healthSystem == key;
    })?.toList();
    if (list == null || list.isEmpty) {
      return PropertyHealthDataInfo(
          '', '', '', 0, '', '', 0, '', 0, '', '', 0, '', '', '', '', '', 0);
    }
    PropertyHealthDataInfo info = list?.first;
    return info;
  }
}
